package userInterface;

import java.awt.Color;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.event.MouseInputListener;

public class RootController implements MouseInputListener
{
	public RootController(RootView view, DrawingModel model)
	{
		_view = view;
		_model = model;
		
		_editingController = new EditingController(_view.getDrawView(), _model);
		_creationController = new CreationController(_view.getDrawView(), _model, this);
		_leftController = new LeftPalleteController(_view.getLeftPalleteView(), _model);
		
		_view.addMouseListener(this);
		_view.addMouseMotionListener(this);

		_curState = RootState.IDLE;
		_curColor = new Color(193, 213, 221);
	}
	
	@Override
	public void mouseClicked(MouseEvent e)
	{
		switch (_curState)
		{
		case IDLE:
			if (e.getComponent().getName().equals("select"))
				_curState = RootState.EDITING;
			else if (isCreationButton(e.getComponent().getName()))
				_curState = RootState.CREATING;
			break;
		case EDITING:
			if (isCreationButton(e.getComponent().getName()))
			{
				_curState = RootState.CREATING;
				_editingController.unfocus();
			}
			else if (e.getComponent().getName().equals("leftView"))
			{
				_curState = RootState.IDLE;
				_editingController.unfocus();
			}
			break;
		case CREATING:
			if (e.getComponent().getName().equals("select"))
				_curState = RootState.EDITING;
			else if (e.getComponent().getName().equals("leftView"))
				_curState = RootState.IDLE;
			break;
		}
		
		if (isLeftViewButton(e.getComponent().getName()))
			_view.getLeftView().setSelectedButton((JButton) e.getComponent());
		else if (e.getComponent().getName().equals("leftView"))
			_view.getLeftView().unselectButton();

		_curLeftPalleteButton = e.getComponent().getName();
	}

	@Override
	public void mouseEntered(MouseEvent e)
	{
		// TODO Auto-generated method stub		
	}

	@Override
	public void mouseExited(MouseEvent e)
	{
		// TODO Auto-generated method stub		
	}

	@Override
	public void mousePressed(MouseEvent e)
	{
		switch (_curState)
		{
		case IDLE:
			break;
		case EDITING:
			if (e.getComponent().getName().equals("drawingView"))
				_editingController.mousePressed(e);
			break;
		case CREATING:				
			if (e.getComponent().getName().equals("drawingView"))
				_creationController.mousePressed(e);
			break;
		}
	}

	@Override
	public void mouseReleased(MouseEvent e)
	{
		switch (_curState)
		{
		case IDLE:
			break;
		case EDITING:
			if (e.getComponent().getName().equals("drawingView"))
				_editingController.mouseReleased(e);
			break;
		case CREATING:
			if (e.getComponent().getName().equals("drawingView"))
				_creationController.mouseReleased(e);
			break;
		}
	}
	
	@Override
	public void mouseDragged(MouseEvent e)
	{
		switch (_curState)
		{
		case IDLE:
			break;
		case EDITING:
			if (e.getComponent().getName().equals("drawingView"))
				_editingController.mouseDragged(e);
			break;
		case CREATING:
			if (e.getComponent().getName().equals("drawingView"))
				_creationController.mouseDragged(e);
			break;
		}
	}

	@Override
	public void mouseMoved(MouseEvent e)
	{
		// TODO Auto-generated method stub
		
	}

	public boolean isLeftViewButton(String s)
	{
		return s.equals("select") || isCreationButton(s);
	}
	
	public boolean isCreationButton(String s)
	{
		return s.equals("line") ||
				s.equals("rectangleFull") ||
				s.equals("rectangleEmpty") ||
				s.equals("ellipseFull") ||
				s.equals("ellipseEmpty") ||
				s.equals("curve") ||
				s.equals("text");
	}
	
	public void setLeftPalleteButton(String str)
	{
		_curLeftPalleteButton = str;
	}
	
	public String getLeftPalleteButton()
	{
		return _curLeftPalleteButton;
	}
	
	public void setSelectedColor(Color c)
	{
		_curColor = c;
		
		// sets the color of the selected object
		if (_view.getDrawView().getSelectedObject() != null)
			_model.getDrawObject(_view.getDrawView().getSelectedObject().getIndex()).setColor(c);
	}

	public Color getSelectedColor()
	{
		return _curColor;
	}
	
	private DrawingModel _model;

	private RootView _view;

	private CreationController _creationController;
	private EditingController _editingController;
	private LeftPalleteController _leftController;
	
	private RootState _curState;

	private String _curLeftPalleteButton;
	private Color _curColor;
}
